package com.gistone.commis.api.restController;

import com.gistone.commis.constant.Status;
import com.gistone.commis.database.GetBySqlMapper;
import com.gistone.commis.dto.*;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.CommunityMapper;
import com.gistone.commis.mapper.DepartMapper;
import com.gistone.commis.mapper.HouseLogMapper;
import com.gistone.commis.mapper.HouseMapper;
import com.gistone.commis.query.QueryHouse;
import com.gistone.commis.util.DataTablePageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/5/5.
 */
@RestController("apiBackNotifyController")
@RequestMapping("api/backNotify")
public class BackNotifyController {

    private static final Logger logger = LoggerFactory.getLogger(BackNotifyController.class);
    @Autowired
    private GetBySqlMapper getBySqlMapper;

    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<Map> pageDatatables(HttpServletRequest request, QueryHouse house) {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");

        String sql = "select * from tb_back_notify where person_id  =" + "'"+user.getUserId()+"'";

        DataTablePageUtil<Map> dataTable = new DataTablePageUtil<>(request);
        PageHelper.startPage(dataTable.getPage_num(), 10);
        List<Map> list =		this.getBySqlMapper.findRecords(sql);
        PageInfo<Map> pageInfo =new PageInfo<>(list);
        System.out.println(list.size());
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面

        return dataTable;

    }

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/remove")
    public Result remove(@RequestParam("id") int id, HttpServletResponse response) {


        String sql = "DELETE FROM tb_back_notify WHERE id = "+ id;

        int no =this.getBySqlMapper.delete(sql);
        if(no>0){
            return  new Result(0,"success","");
        }else{
            return  new Result(1,"error","");
        }




    }



}
